package com.tenglong.teacher.ui.adapter.viewholder;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.cuotibao.zhuoxiao.basics.utils.IpUtils;
import com.cuotibao.zhuoxiao.basics.utils.ToastUtils;
import com.cuotibao.zhuoxiao.basics.utils.glide.GlideOptions;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.MessageInfo;
import com.tenglong.teacher.data.resultInfo.VerifyMessageInfo;
import com.tenglong.teacher.data.resultInfo.base.BaseResult;
import com.tenglong.teacher.data.resultInfo.base.ResultVerifier;
import com.tenglong.teacher.services.hdx.CreateRequestEntity;
import com.tenglong.teacher.ui.adapter.MessageAdapter;
import com.tenglong.teacher.ui.adapter.viewholder.base.BaseViewHolder;
import com.thinkcool.circletextimageview.CircleTextImageView;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author hdx on 2019/4/15.
 */
public class VerifyMessageViewHolder extends BaseViewHolder<MessageInfo> implements View.OnClickListener {
    private MessageAdapter.MessageListener listener;
    private CircleTextImageView image;
    private TextView name, reject, agree, classInfo, status;
    private VerifyMessageInfo data;
    private RecyclerView.Adapter mAdapter;

    public VerifyMessageViewHolder(View itemView) {
        super(itemView);
    }

    public VerifyMessageViewHolder(View itemView, MessageAdapter.MessageListener listener, RecyclerView.Adapter adapter) {
        this(itemView);
        this.mAdapter = adapter;
        this.listener = listener;
        initView();
        setListeners();
    }

    private void setListeners() {
        reject.setOnClickListener(this);
        agree.setOnClickListener(this);
    }

    private void initView() {
        image = itemView.findViewById(R.id.image);
        name = itemView.findViewById(R.id.name);
        classInfo = itemView.findViewById(R.id.classInfo);
        reject = itemView.findViewById(R.id.reject);
        agree = itemView.findViewById(R.id.agree);
        status = itemView.findViewById(R.id.status);
    }

    @Override
    public void setLayoutInfo(MessageInfo info) {
        if (info instanceof VerifyMessageInfo) {
            this.data = (VerifyMessageInfo) info;
            RequestOptions options = GlideOptions.getGlideRequestOptions().
                    error(R.drawable.setting_image_defauft_header).
                    placeholder(R.drawable.header_image_loading);
            Glide.with(mContext)
                    .load(IpUtils.getImageUrl(data.photoUrl))
                    .apply(options)
                    .into(image);
            name.setText(data.studentName == null ? "" : data.studentName);
            classInfo.setText("加入" + data.className);
            agree.setVisibility(data.status == 0 ? View.VISIBLE : View.GONE);
            reject.setVisibility(data.status == 0 ? View.VISIBLE : View.GONE);
            status.setVisibility(data.status == 0 ? View.GONE : View.VISIBLE);
            if (status.getVisibility() == View.VISIBLE) {
                switch (data.status) {
                    case 0:
                        break;
                    case 1:
                        status.setText("已同意");
                        break;
                    default:
                        status.setText("已拒绝");
                        break;
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.agree:
                if (listener != null) {
                    listener.onVerifyMessageEvent(getLayoutPosition(), true);
                }
                break;
            case R.id.reject:
                if (listener != null) {
                    listener.onVerifyMessageEvent(getLayoutPosition(), false);
                }
                break;
        }
    }

}
